
#include "blockingsocket.h"

BlockingSocket::BlockingSocket(QObject* parent): QTcpSocket(parent)
{
	m_timeout=3000;
}

void BlockingSocket::setTimeout(int timeout)
{
	m_timeout=timeout;
}

qint64 BlockingSocket::readData(char* data, qint64 maxlen)
{
        QTime m_time;
        m_time.start();
        while (bytesAvailable() < maxlen)
                if (m_time.elapsed()>m_timeout || !waitForReadyRead(m_timeout-m_time.elapsed()))
                        break;

        return QTcpSocket::readData(data, maxlen);

}

qint64 BlockingSocket::writeData(const char* data, qint64 len)
{
	qint64 res=QAbstractSocket::writeData(data, len);
	if(state()!=QAbstractSocket::UnconnectedState)
		waitForBytesWritten(m_timeout);
	return res;
}

